﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;
namespace cczEdit.DramaDialog
{
    public partial class EditDramaChangeAIDialog : Form
    {
        List<Hero> heros;
        PListDict curDict;
        public EditDramaChangeAIDialog()
        {
            InitializeComponent();
            heros = UCDrama.heros;
            foreach (Hero hero in heros)
            {
                comboBoxOriginHero.Items.Add(hero.GetName());
                comboBoxTargetHero.Items.Add(hero.GetName());
            }
            foreach (KeyValuePair<int, string> kv in Constant.aiType)
            {
                comboBoxAIType.Items.Add(kv.Value);
            }
            foreach (KeyValuePair<int, string> kv in Constant.ownTypeDic)
            {
                comboBoxOwner.Items.Add(kv.Value);
            }
        }
        
        public void Show(PListDict dict,IWin32Window owner)
        {
            curDict = dict;
            PListInteger isZone = (PListInteger)dict["IsZone"];
            comboBoxZoneType.SelectedIndex = (int)isZone.Value;
            PListInteger type = (PListInteger)dict["Type"];
            comboBoxAIType.SelectedIndex = (int)type.Value;
            PListDict unitDict = (PListDict)dict["UnitDic"];
            PListInteger unitID = (PListInteger)unitDict["UnitID"];
            PListInteger originX = (PListInteger)unitDict["OriginX"];
            PListInteger originY = (PListInteger)unitDict["OriginY"];
            PListInteger dstX = (PListInteger)unitDict["DstX"];
            PListInteger dstY = (PListInteger)unitDict["DstY"];
            PListInteger unitOwner = (PListInteger)unitDict["Owner"];
            tbOriginX.Text = originX.Value.ToString();
            tbOriginY.Text = originY.Value.ToString();
            tbToX.Text = dstX.Value.ToString();
            tbToY.Text = dstY.Value.ToString();
            comboBoxOwner.SelectedIndex =(int) unitOwner.Value;
            Hero originHero = heros.Find((h)=>{return h.num==unitID.Value;});
            comboBoxOriginHero.SelectedItem = originHero.GetName();
            PListDict targetDic = (PListDict)dict["TargetDic"];
            PListInteger targetUnitID = (PListInteger)targetDic["UnitID"];
            PListInteger coordX = (PListInteger)targetDic["CoordX"];
            PListInteger coordY = (PListInteger)targetDic["CoordY"];
            tbTargetX.Text = coordX.Value.ToString();
            tbTargetY.Text = coordY.Value.ToString();
            Hero targetHero = heros.Find((h) => { return h.num == targetUnitID.Value; });
            comboBoxTargetHero.SelectedItem = targetHero.GetName();
            this.ShowDialog(owner);
        }
        private void buttonAccept_Click(object sender, EventArgs e)
        {
            curDict["IsZone"] = new PListInteger(comboBoxZoneType.SelectedIndex);
            curDict["Type"] = new PListInteger(comboBoxAIType.SelectedIndex);
            PListDict unitDict = (PListDict)curDict["UnitDic"];
            if (comboBoxOriginHero.Text != null)
            {
                Hero originHero = heros.Find((h) => { return h.GetName() == comboBoxOriginHero.Text.ToString(); });
                if(originHero!=null)
                    unitDict["UnitID"] = new PListInteger(originHero.num);
                else
                    unitDict["UnitID"] = new PListInteger(0);
            }
            unitDict["OriginX"] = new PListInteger(System.Int32.Parse(tbOriginX.Text));
            unitDict["OriginY"] = new PListInteger(System.Int32.Parse(tbOriginY.Text));
            unitDict["DstX"] = new PListInteger(System.Int32.Parse(tbToX.Text));
            unitDict["DstY"] = new PListInteger(System.Int32.Parse(tbToY.Text));
            unitDict["Owner"] = new PListInteger(comboBoxOwner.SelectedIndex);

            PListDict targetDic = (PListDict)curDict["TargetDic"];
            Hero targetHero = heros.Find((h) => { return h.GetName() == comboBoxTargetHero.Text.ToString(); });
            if (targetHero != null)
            {
                targetDic["UnitID"] = new PListInteger(targetHero.num);
            }
            targetDic["CoordX"] = new PListInteger(System.Int32.Parse(tbTargetX.Text));
            targetDic["CoordY"] = new PListInteger(System.Int32.Parse(tbTargetY.Text));
            this.Hide();

        }

        private void comboBoxZoneType_SelectedIndexChanged(object sender, EventArgs e)
        {
            label2.Visible = true;
            comboBoxOriginHero.Visible = true;
            groupBox1.Visible = true;
            if (comboBoxZoneType.SelectedIndex == 0)
            {
                groupBox1.Visible = false;
            }
            else
            {
                label2.Visible = false;
                comboBoxOriginHero.Visible = false;
            }
        }

        private void comboBoxAIType_SelectedIndexChanged(object sender, EventArgs e)
        {
            label7.Visible = false;
            tbTargetX.Visible = false;
            tbTargetY.Visible = false;
            label8.Visible = false;
            comboBoxTargetHero.Visible = false;
            if (comboBoxAIType.SelectedIndex == (int)kAIType.kAITypeAttackUnit 
                || comboBoxAIType.SelectedIndex == (int)kAIType.kAITypeFollowUnit)
            {
                label8.Visible = true;
                comboBoxTargetHero.Visible = true;
            }
            else if(comboBoxAIType.SelectedIndex>2)
            {
                label7.Visible = true;
                tbTargetX.Visible = true;
                tbTargetY.Visible = true;
            }
        }
    }
}
